function bindthree() {
  var i;
  var k;
  var j;
  var app=getApp();
  app.globalData.sign = false;
  i = Math.floor(Math.random() * 5);
  if(i == 0){//一位数乘两，三位
    app.globalData.addend1 = Math.floor(Math.random() * 9) + 1;
    app.globalData.addend2 = Math.floor(Math.random() * 990) + 10;
    app.globalData.answer = (app.globalData.addend1 * app.globalData.addend2);
    app.globalData.question = (app.globalData.addend1 + " × " + app.globalData.addend2 + " = ");
  }
  if(i == 1){//两位数乘两位数
    app.globalData.addend1 = Math.floor(Math.random() * 90) + 10;
    app.globalData.addend2 = Math.floor(Math.random() * 90) + 10;
    app.globalData.answer = (app.globalData.addend1 * app.globalData.addend2);
    app.globalData.question = (app.globalData.addend1 + " × " + app.globalData.addend2 + " = ");
  }
  if(i == 2){//乘除加减两步混合（除法保证整除）
    j = Math.floor(Math.random() * 4);
    if(j == 0){//加乘
      app.globalData.addend1 = Math.floor(Math.random() * 99) + 1;
      k = 100 - app.globalData.addend1;
      app.globalData.addend2 = Math.floor(Math.random() * k) + 1;
      app.globalData.addend3 = Math.floor(Math.random() * 10);
      while((app.globalData.addend1 + app.globalData.addend2) * app.globalData.addend3 >100){
        app.globalData.addend3 = Math.floor(Math.random() * 10);
      }
      app.globalData.answer = (app.globalData.addend1 + app.globalData.addend2) * app.globalData.addend3;
      app.globalData.question = ("(" + app.globalData.addend1 + " + " + app.globalData.addend2 + ")" + " × " +  app.globalData.addend3 + " = ");
    }
    else if(j == 1){//加除
      app.globalData.sign = true;
      app.globalData.addend1 = Math.floor(Math.random() * 99) + 1;
      k = 100 - app.globalData.addend1;
      app.globalData.addend2 = Math.floor(Math.random() * k) + 1;
      app.globalData.addend3 = Math.floor(Math.random() * 19) + 1;
      while((app.globalData.addend1 + app.globalData.addend2) % app.globalData.addend3 != 0){
        app.globalData.addend3 = Math.floor(Math.random() * 20);
      }
      app.globalData.answer = (app.globalData.addend1 + app.globalData.addend2) / app.globalData.addend3;
      app.globalData.question = ("(" + app.globalData.addend1 + " + " + app.globalData.addend2 + ")" + " ÷ " +  app.globalData.addend3 + " = ");
    }
    else if(j == 2){//减乘
      app.globalData.addend1 = Math.floor(Math.random() * 99) + 1;
      app.globalData.addend2 = Math.floor(Math.random() * app.globalData.addend1);
      app.globalData.addend3 = Math.floor(Math.random() * 10);
      while((app.globalData.addend1 - app.globalData.addend2) * app.globalData.addend3 >100){
        app.globalData.addend3 = Math.floor(Math.random() * 10);
      }
      app.globalData.answer = (app.globalData.addend1 - app.globalData.addend2) * app.globalData.addend3;
      app.globalData.question = ("(" + app.globalData.addend1 + " - " + app.globalData.addend2 + ")" + " × " +  app.globalData.addend3 + " = ");
    }
    else{//减除
      app.globalData.sign = true;
      app.globalData.addend1 = Math.floor(Math.random() * 99) + 1;
      app.globalData.addend2 = Math.floor(Math.random() * app.globalData.addend1);
      app.globalData.addend3 = Math.floor(Math.random() * 4) + 1;
      while((app.globalData.addend1 - app.globalData.addend2) % app.globalData.addend3 != 0){
        app.globalData.addend3 = Math.floor(Math.random() * 4) + 1;
      }
      app.globalData.answer = (app.globalData.addend1 - app.globalData.addend2) / app.globalData.addend3;
      app.globalData.question = ("(" + app.globalData.addend1 + " - " + app.globalData.addend2 + ")" + " ÷ " +  app.globalData.addend3 + " = ");
    }
  }
  if(i == 3){//一位数除两三位数
    app.globalData.sign = true;
    app.globalData.addend1 = Math.floor(Math.random() * 990) + 10;
    app.globalData.addend2 = Math.floor(Math.random() * 9) + 1;
    while(app.globalData.addend1 % app.globalData.addend2 != 0){
      app.globalData.addend2 = Math.floor(Math.random() * 9) + 1;
    }
    app.globalData.answer = Math.floor(app.globalData.addend1 / app.globalData.addend2);
    app.globalData.question = (app.globalData.addend1 + " ÷ " + app.globalData.addend2 + " = ");
  }
  if(i == 4){//万以内数加减混合运算
    app.globalData.addend1 = Math.floor(Math.random() * 10000);
    k = 10000 - app.globalData.addend1;
    app.globalData.addend2 = Math.floor(Math.random() * k);
    app.globalData.addend3 = Math.floor(Math.random() * k);
    if((app.globalData.addend1+app.globalData.addend2+app.globalData.addend3)<10000){
      app.globalData.answer = (app.globalData.addend1+app.globalData.addend2+app.globalData.addend3);
      app.globalData.question = (app.globalData.addend1 + " + " + app.globalData.addend2 + " + " + app.globalData.addend3 + " = ");
    }
    else if((app.globalData.addend1+app.globalData.addend2-app.globalData.addend3)<10000 &&(app.globalData.addend1+app.globalData.addend2-app.globalData.addend3)>0){
      app.globalData.answer = (app.globalData.addend1+app.globalData.addend2-app.globalData.addend3);
      app.globalData.question = (app.globalData.addend1 + " + " + app.globalData.addend2 + " - " + app.globalData.addend3 + " = ");
    }
    else{
      app.globalData.answer = (app.globalData.addend1-app.globalData.addend2+app.globalData.addend3);
      app.globalData.question = (app.globalData.addend1 + " - " + app.globalData.addend2 + " + " + app.globalData.addend3 + " = ");
    }
  }
}

Page({
  /**
   * 页面的初始数据
   */
  data: {
    data_input1:{},
    data_input2:{},
    cir:"",
    r_n:0,
    a_n:0,
    num:15,
    time:30,
    answer:0,
    remainder:0,
    modalHidden:true,
    sign:false,
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.Numdown()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function(){
    if(this.data.num=="无"||this.data.time=="测试结束"){
      this.setData({
        num:15,
        time:90,
        r_n:0,
        a_n:0,
      })
      this.Numdown()
    }
    wx.setNavigationBarTitle({
      title: '测验 三年级'
    })
    bindthree();
    this.setData({
      answer:getApp().globalData.answer,
      cir:getApp().globalData.question,
      remainder:getApp().globalData.remainder,
      sign:getApp().globalData.sign,
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onUnload:function(){
    if(this.data.num != "无") {
      this.setData({
        num:0,
      })
      this.endNum();
    }
  },
  Numdown: function() {
    var that = this;
    var time = that.data.time;
    var num = that.data.num;
    setTimeout(function() {
        that.setData({
          time:time-1
        })
        that.endNum();

    }, 1000)
  },
  endNum: function() {
    var that = this,
    num = that.data.num;
    var time = this.data.time
    if (num == 0||time == 0) {
      that.setData({
        num: "无",
        time:"测试结束"
      })
      console.log(this.data.time);
      var str;
      if(this.data.a_n == 0) str = "你没有完成任何试题"
      else str = (this.data.r_n/this.data.a_n).toFixed(2);
      wx.showModal({
        title: '测试结束',
        content:'完成题目：'+this.data.a_n+'\n'+'正确题目：'+this.data.r_n+'\n'+'正确率：'+str,
        success: function(res) {
          if(res.cancel){
          //这个跳转是左边按钮的跳转链接
            wx.switchTab({
              url: '../test/test'
            })
          }else{
          //这里是右边按钮的跳转链接
            wx.switchTab({
              url: '../test/test'
            })
          }
        }
      })
    } else{
      if(this.data.num == "无"){
        var that = this;
        that.setData({
          time: "测试结束"
        })
      }
      else{
        var that = this;
        that.Numdown();
      }
    }
  },

  datain1: function(e){
    this.setData({
      data_input1:e.detail.value,
    });
  },
  datain2: function(e){
    this.setData({
      data_input2:e.detail.value,
    });
  },
  submit:function(){
    this.data.num--;
    console.log("num:"+this.data.num)
    try{
      getApp().data.yop = wx.getStorageSync('wrong_n')
    }catch(e){
      getApp().data.yop = 0;
    }
    getApp().data.yop++,
    this.data.a_n++;
    wx.setStorage({
      data: getApp().data.yop,
      key: "wrong_n",
    })
    var num=(getApp().data.yop-1) % 30;
    try{
      getApp().data.data1 = wx.getStorageSync('his')
      if(!getApp().data.data1) getApp().data.data1 = [];
    }catch(e){
    }
    if(this.data.sign){
      getApp().data.data1[num]=this.data.cir+this.data.answer+'...'+this.data.remainder;
    }
    else{
      getApp().data.data1[num]=this.data.cir+this.data.answer;
    }
    wx.setStorage({
      data: getApp().data.data1,
      key: "his",
    })
    var x=(this.data.answer==this.data.data_input1);
    var y=(this.data.remainder==this.data.data_input2);
    if(((!this.data.sign)&&x)||((this.data.sign)&&x&&y)){
      this.data.r_n++;
    } else{
      try{
        getApp().data.xop = wx.getStorageSync('right_n')
      }catch(e){
      }
      getApp().data.xop++;
      wx.setStorage({
        data: getApp().data.xop,
        key: "right_n",
      })
      var num=(getApp().data.xop-1) % 30;
      try{
        getApp().data.data2 = wx.getStorageSync('wro')
        if(!getApp().data.data2) getApp().data.data2 = [];
      }catch(e){
      }
      if(this.data.sign){
        getApp().data.data2[num]=this.data.cir+this.data.answer+'...'+this.data.remainder;
      }
      else{
        getApp().data.data2[num]=this.data.cir+this.data.answer;
      }
      wx.setStorage({
        data: getApp().data.data2,
        key: "wro",
      })
      this.setData({
        modalHidden:!this.data.modalHidden
      })
    }
    if(this.data.num == 0){
      var that = this;
      that.endNum();
    } 
    else{
      var that=this;
      that.onShow();
    }
  },
})